import getColorArr from "@/utils/color";
import moment from "moment";
import api_option from "@/api/blog/option";
import { getDict, setDict } from "./local";
//获取dom style
export const getStyle = (domName: string, domQuery: string): any => {
    const dom: any = document.querySelector(domName);
    if (dom) {
        let style: any = window.getComputedStyle(dom);
        return style[domQuery];
    }
};
//设置dom style
export const setStyle = (domName: string, domQuery: string, domQueryValue: string): void => {
    const dom: any = document.querySelector(domName);
    if (dom) {
        dom.style[domQuery] = domQueryValue;
    }
};
//设置主题色
export const setTheme = (colorName: string, color: string): void => {
    const el = document.documentElement;
    const oldColor = getComputedStyle(el).getPropertyValue(`--c-base`).trim();
    let gradient: any = getColorArr.gradientColor(color, oldColor, 10);
    gradient.forEach((item: string, index: number) => {
        if (index === 0) el.style.setProperty(`${colorName}`, item);
        if (index === 9) return;
        el.style.setProperty(`${colorName}-light-${index + 1}`, item);
    });
};
//获取已经过去的时间
export const formatTimeLater = (date: any) => {
    date = moment(date).valueOf();
    const ONE_SECOND = 1000;
    const ONE_MINUTE = 60000;
    const ONE_HOUR = 3600000;
    const ONE_DAY = 86400000;
    const ONE_WEEK = 604800000;
    const ONE_MONTH = 2629746000;
    const ONE_YEAR = 31556952000;
    const ONE_SECOND_AGO = "秒前";
    const ONE_MINUTE_AGO = "分钟前";
    const ONE_HOUR_AGO = "小时前";
    const ONE_DAY_AGO = "天前";
    const ONE_MONTH_AGO = "月前";
    const ONE_YEAR_AGO = "年前";
    let delta = new Date().getTime() - date;
    if (delta < 1 * ONE_MINUTE) {
        let seconds = ~~(delta / ONE_SECOND);
        return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
    }
    if (delta < 45 * ONE_MINUTE) {
        let minutes = ~~(delta / ONE_MINUTE);
        return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
    }
    if (delta < 24 * ONE_HOUR) {
        let hours = ~~(delta / ONE_HOUR);
        return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
    }
    if (delta < 48 * ONE_HOUR) {
        return "昨天";
    }
    if (delta < 30 * ONE_DAY) {
        let days = ~~(delta / ONE_DAY);
        return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
    }
    if (delta < 12 * 4 * ONE_WEEK) {
        let months = ~~(delta / ONE_MONTH);
        return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
    } else {
        let years = ~~(delta / ONE_YEAR);
        return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;
    }
};
//登出
export const loginOut = (): void => {
    localStorage.clear();
};
// 字典中根据"type,value"获取name
export const getOptNameType = (type: string, value: string) => {
    try {
        let name = "";
        const dict = getDict();
        let option = dict[type];
        let index = option.findIndex((item: any) => item.value === value);
        name = index !== -1 ? option[index]["label"] : "不存在";
        return name;
    } catch (err) {
        return '不存在';
    }
};
//获取字典[]
export const getAllDict = async (typeArrString: string) => {
    if (!typeArrString) return;
    let res = await api_option.getOptionOr({ type: typeArrString });
    setDict(res.data);
};

export default {
    getOptNameType,
    loginOut,
    getAllDict,
    getStyle,
    setStyle,
    setTheme,
    formatTimeLater,
};
